package com.sphd.emanu.activity.financestaff;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sphd.emanu.R;
import com.sphd.emanu.activity.addcard.AddCardActivity;
import com.sphd.emanu.activity.cardmessage.CardMessageActivity;
import com.sphd.emanu.adapter.BankListAdapter;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.mvp.MVPBaseActivity;
import com.sphd.emanu.permission.Moudle;
import com.sphd.emanu.pojo.bank.BankListInfo;
import com.sphd.emanu.pojo.login.User;
import com.sphd.emanu.utils.PermissionConfig;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 财务模块---财务人员
 *
 * @Author Hu
 * @Time 2017/5/15 -- 16:39.
 * @Email 87683202@qq.com
 */
public class FinancestaffActivity extends MVPBaseActivity<FinancestaffContract.View, FinancestaffPresenter> implements FinancestaffContract.View {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.my_recycler)
    RecyclerView myRecycler;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    @BindView(R.id.menu_img)
    ImageView menuImg;
    User user;
    Bundle bundle;
    Intent intent;
    BankListAdapter bankListAdapter;
    boolean isCanDo=false;
    List<Moudle> moudles;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_financestaff);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(v -> finish());
        myRecycler.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                BankListInfo bankListInfo = (BankListInfo) adapter.getItem(position);

                bundle.putSerializable(Constant.BANKLISTINFO, bankListInfo);
                intent.putExtras(bundle);
                intent.setClass(context, CardMessageActivity.class);//账号信息

                startActivity(intent);
            }
        });
    }

    private void initView() {
        setTitleBar(toolbar, "财务");
        intent = getIntent();
        bundle = intent.getExtras();
        user = (User) bundle.getSerializable(Constant.USER_ENTITY);
        moudles= (List<Moudle>) bundle.getSerializable(Constant.PERMISSION_LIST);
        isCanDo=isCanDo(moudles, PermissionConfig.MENU_FINANCE_5);
        if (isCanDo){
            // TODO: 2017/7/25  待到有权限的时候记得处理这个地方的显示
            menuImg.setVisibility(View.VISIBLE);
            menuImg.setImageResource(R.mipmap.add);

        }

        setRefresh(mySwf);
        initAdapter();
        addData();

    }

    private void initAdapter() {
        setRecy(myRecycler);
        bankListAdapter = new BankListAdapter(R.layout.layout_bankcard_item, new ArrayList<BankListInfo>(), this);
        bankListAdapter.setEmptyView(emptyView);
        myRecycler.setAdapter(bankListAdapter);

    }

    private void addData() {
        showProgressDialog();
        mPresenter.financialManger(user.getOid());
    }

    @OnClick(R.id.menu_img)
    public void onViewClicked() {
        bundle.putBoolean(Constant.IS_ADD,true);
        intent.putExtras(bundle);
        intent.setClass(context, AddCardActivity.class);
        startActivity(intent);
    }

    @Override
    public void setListBean(List<BankListInfo> body) {
        dissProgressDialog();
        bankListAdapter.setNewData(body);
    }
}
